// 获取元素
const playBtn = document.getElementById('playBtn');
const pauseBtn = document.getElementById('pauseBtn');
const progressBar = document.getElementById('progressBar');
const currentTime = document.getElementById('currentTime');
const totalTime = document.getElementById('totalTime');
const playlist = document.getElementById('playlist');
const audioPlayer = document.getElementById('audioPlayer');
const resetList = document.getElementById('resetList');
const shuffleList = document.getElementById('shuffleList');
const clearList = document.getElementById('clearList');

// 播放/暂停功能
playBtn.addEventListener('click', () => {
  audioPlayer.play();
  playBtn.style.display = 'none';
  pauseBtn.style.display = 'block';
});

pauseBtn.addEventListener('click', () => {
  audioPlayer.pause();
  playBtn.style.display = 'block';
  pauseBtn.style.display = 'none';
});

// 进度条功能
audioPlayer.addEventListener('timeupdate', () => {
  const percent = (audioPlayer.currentTime / audioPlayer.duration) * 100;
  progressBar.value = percent;
  currentTime.textContent = formatTime(audioPlayer.currentTime);
});

progressBar.addEventListener('input', () => {
  const time = (progressBar.value / 100) * audioPlayer.duration;
  audioPlayer.currentTime = time;
});

// 格式化时间函数
function formatTime(seconds) {
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = Math.floor(seconds % 60);
  return `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
}

// 加载音频时获取总时长
audioPlayer.addEventListener('loadedmetadata', () => {
  totalTime.textContent = formatTime(audioPlayer.duration);
});

// 播放列表点击切换歌曲
playlist.addEventListener('click', (e) => {
  if (e.target.tagName === 'LI') {
    const li = e.target;
    const src = li.dataset.src;
    audioPlayer.src = src;
    audioPlayer.load();
    audioPlayer.play();
    const activeLi = document.querySelector('#playlist li.active');
    if (activeLi) {
      activeLi.classList.remove('active');
    }
    li.classList.add('active');
    playBtn.style.display = 'block';
    pauseBtn.style.display = 'none';
  }
});

// 重置列表（暂未实现具体逻辑，可按需求补充）
resetList.addEventListener('click', () => {
  console.log('重置列表功能待完善');
});

// 随机打乱列表（暂未实现具体逻辑，可按需求补充）
shuffleList.addEventListener('click', () => {
  console.log('随机打乱功能待完善');
});

// 清空列表（暂未实现具体逻辑，可按需求补充）
clearList.addEventListener('click', () => {
  console.log('清空列表功能待完善');
});